package com.turing.procedure.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.turing.common.response.ResponseCode;
import com.turing.common.response.ResponseMessage;
import com.turing.common.response.ResponseVO;

import com.turing.procedure.client.reviewer.ReviewTaskInnerService;
import com.turing.procedure.model.domain.task.TaskRecord;
import com.turing.procedure.service.task.StatisticService;
import com.turing.procedure.service.task.TaskRecordService;
import com.turing.procedure.vo.in.KeyStatisticSearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/statistic")
public class StatisticController {

    @Autowired
    private StatisticService statisticService;

    @Autowired
    private ReviewTaskInnerService reviewTaskInnerService;

    @Autowired
    private TaskRecordService taskRecordService;

    @PostMapping("keyStatistic")
    public ResponseVO< Map<String, String> > keyStatisticByRange(@RequestBody KeyStatisticSearchVO searchVO ){

        Map< String, String > keyStatistic = statisticService.getKeyStatistic( searchVO );

        return new ResponseVO<>( keyStatistic, ResponseCode.SUCCESS, ResponseMessage.SUCCESS, 1 );

    }

    @PostMapping("graphData")
    public ResponseVO< Map<String, List<Object> >> getGraphData(String templateSn, Integer eleId, @RequestParam("dateType") Integer dateType ){
        try {
            Map< String, List< Object > > graphData = statisticService.getGraphData( templateSn, eleId, dateType );
            return new ResponseVO<>( graphData, ResponseCode.SUCCESS, ResponseMessage.SUCCESS, 1 );
        } catch ( ParseException e ) {
            e.printStackTrace();
            return new ResponseVO<>( ResponseCode.SYSTEM_ERROR, ResponseMessage.SYSTEM_ERROR,1 );
        }
    }

    @PostMapping("writeEleResult")
    public void writeEleResult(){

        List<TaskRecord> taskRecords = taskRecordService.list( new LambdaQueryWrapper<TaskRecord>().eq(TaskRecord::getIsDel,0)
            .in(TaskRecord::getStatus,5,6));

        SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        for ( TaskRecord taskRecord : taskRecords) {
            String taskSn = taskRecord.getTaskSn();
            Date uploadTime = taskRecord.getUploadTime();

            reviewTaskInnerService.writeReviewedResult( taskSn, format.format( uploadTime ) );
        }
    }
}
